package com.kyzh.core.uis

import android.content.Context
import android.text.Editable
import android.text.InputType
import android.text.TextWatcher
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.widget.EditText
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.kyzh.core.R

class SuperView : LinearLayout {

    private var canEdit = true
    private var hasArrow = true
    private var hasLine = true
    private var startText = ""
    private var endHint = ""
    private var inputType = 100
    private var maxEms = 100000
    private var tvDesc: TextView? = null
    private var etContent: EditText? = null
    private var arrow: ImageView? = null
    private var line: View? = null

    constructor(context: Context) : this(context, null, 0)
    constructor(context: Context, attributes: AttributeSet) : this(context, attributes, 0)
    constructor(context: Context, attributes: AttributeSet?, defStyle: Int) : super(context, attributes, defStyle) {
        val typeArray = context.obtainStyledAttributes(attributes, R.styleable.SuperView)
        canEdit = typeArray.getBoolean(R.styleable.SuperView_canEdit, false)
        hasArrow = typeArray.getBoolean(R.styleable.SuperView_hasArrow, true)
        hasLine = typeArray.getBoolean(R.styleable.SuperView_hasLine, true)
        startText = typeArray.getString(R.styleable.SuperView_startText) ?: ""
        endHint = typeArray.getString(R.styleable.SuperView_endHint) ?: ""
        inputType = typeArray.getInt(R.styleable.SuperView_inputType, 100)
        maxEms = typeArray.getInt(R.styleable.SuperView_maxEms, 1000000)
        initView(context)
    }

    private fun initView(context: Context) {
        LayoutInflater.from(context).inflate(R.layout.super_view_item, this, true)
        tvDesc = findViewById(R.id.tvDesc)
        etContent = findViewById(R.id.etContent)
        arrow = findViewById(R.id.ivArrow)
        line = findViewById(R.id.line)

        etContent?.isFocusable = canEdit
        etContent?.isFocusableInTouchMode = canEdit
        etContent?.maxEms = maxEms

        arrow?.visibility = if (hasArrow) View.VISIBLE else View.INVISIBLE
        line?.visibility = if (hasLine) View.VISIBLE else View.INVISIBLE
        tvDesc?.text = startText
        etContent?.hint = endHint
        if (inputType == 0) {
            etContent?.inputType = InputType.TYPE_CLASS_NUMBER
        }
    }

    fun getContent(): String {
        return etContent?.text.toString()
    }

    fun setEnable(boolean: Boolean){
        etContent?.isEnabled=boolean
    }
    fun setEndText(text: String) {
        etContent?.setText(text)
    }
    fun setEndHintText(text: String) {
        etContent?.hint = text
    }

    fun setOnTextChangeListener(textChangeListener: textChangeListener) {
        etContent?.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {
                //如果EditText中的数据不为空，且长度大于指定的最大长度
                if (!s.isNullOrEmpty() && s.length > maxEms) {
                    //删除指定长度之后的数据
                    s.delete(maxEms, etContent!!.getSelectionEnd());
                }
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                val string = s.toString()

                textChangeListener.onchange(string)
            }
        })
    }


    interface textChangeListener {
        fun onchange(string: String)
    }

    override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {
        return !canEdit
    }
}